#include"instruction_mem.h"

bool Instruction_Mem::store_instruction(addr_size_t addr, instruction* instr, int para_nums,addr_size_t head_addr) {
    auto it = this->instruction_memory->find(addr);
    if (it != this->instruction_memory->end()) {
        //如果同一个地址出现了多个指令，则报错，返回false
        return false;
    } else {
        instr_unit* iu = new instr_unit();
        iu->instr = instr;
        iu->head_addr = head_addr;
        iu->para_instr_nums = para_nums;
        this->instruction_memory->insert(std::make_pair(addr, iu));
        return true;
    }
}

instr_unit* Instruction_Mem::get_instr(addr_size_t addr) {
    auto it = this->instruction_memory->find(addr);
    if (it != this->instruction_memory->end()) {
        return it->second;
    } else {
        //如果没有找到该地址对应的指令，则返回空指针
        return NULL;
    }
}

bool Instruction_Mem::store_function(std::string func_name, addr_size_t head_addr, addr_size_t end_addr){
    // std::cout << "Attempting to store function: " << func_name << " with head address: " << std::hex << head_addr << " and end address: " << end_addr << std::dec << std::endl;
    auto it = this->function_list->find(func_name);
    // std::cout << "Function list size: " << this->function_list->size() << std::endl; // 输出当前函数列表的大小
    if (it != this->function_list->end()) {
        //如果同一个地址出现了多个指令，则报错，返回false
        return false;
    } else {
        // std::cout << "Storing new function: " << func_name << std::endl; // 输出正在存储的新函数名
        addr_unit* iu = new addr_unit();
        // std::cout << "Storing function: " << func_name << " with head address: " << std::hex << head_addr << " and end address: " << end_addr << std::dec << std::endl;
        iu->head_addr = head_addr;
        iu->end_addr = end_addr;

        this->function_list->insert(std::make_pair(func_name, iu));
        return true;
    }
    
    
}
    
addr_unit* Instruction_Mem::get_func(std::string func_name){
    auto it = this->function_list->find(func_name);
    if (it != this->function_list->end()) {
        return it->second;
    } else {
        //如果没有找到该地址对应的指令，则返回空指针
        return NULL;
    }
};